package com.hpf.community.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.hpf.community.annotations.MonitorLog;
import com.hpf.community.constants.CommunityConstants;
import com.hpf.community.enums.LogEnum;
import com.hpf.community.model.ClassModel;
import com.hpf.community.service.IClassService;
import com.hpf.community.utils.CommonUtils;
import com.hpf.community.utils.PaginationResult;
import com.hpf.community.utils.StandardResult;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;


/**
 *　　
 *   Class 控制器    ${table.comment}
 *
 *   @author huangpengfei
 *   @since 2020-04-04
 */

@RestController
public class ClassController {
    private final Logger logger = LoggerFactory.getLogger(ClassController.class);

    @Autowired
    public IClassService classService;

    /**
     * 获取分页列表
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @GetMapping("/class")
    @RequiresRoles("3")
    public StandardResult selectPage(@ModelAttribute ClassModel classModel, Integer pageSize, Integer pageNumber,String searchKey) {
		try {
			if (pageSize == null || pageNumber == null) {
				return StandardResult.faildCheck("缺少必要的分页参数！");
			}
	     	Page<ClassModel> page = new Page<ClassModel>(pageNumber, pageSize);
	     	Wrapper<ClassModel> wrapper = new EntityWrapper<ClassModel>();
	     	if(StringUtils.isNotBlank(searchKey)){
	     	    wrapper.like(ClassModel.NAME,searchKey);
	     	    wrapper.or();
	     	    wrapper.like(ClassModel.FACULTY,searchKey);
	     	    wrapper.or();
	     	    wrapper.like(ClassModel.MAJOR,searchKey);
	     	}
	     	if(StringUtils.isNotBlank(classModel.getStatus())){
	     	    wrapper.andNew();
	     	    wrapper.eq(ClassModel.STATUS,classModel.getStatus());
	     	}
	     	classService.selectPage(page, wrapper);
			return PaginationResult.ok(null, page.getRecords(), page.getTotal(), page.getPages());
		} catch (Exception e) {
			logger.error("异常信息:", e);
			return PaginationResult.faild(e);
		}
    }

	/**
     * 获取列表
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @GetMapping("/classList")
    public StandardResult selectList(@ModelAttribute ClassModel classModel,Boolean isFaculty) {
		try {
			Wrapper<ClassModel> wrapper = new EntityWrapper<ClassModel>();
			if(isFaculty){
			    //获取院系列表
			    wrapper.setSqlSelect("FACULTY AS faculty");
                wrapper.groupBy(ClassModel.FACULTY);
			}
            wrapper.eq(ClassModel.STATUS, CommunityConstants.USER_STATUS_ZC);
			wrapper.isNotNull(ClassModel.NAME);
			return StandardResult.ok(null, classService.selectList(wrapper));
		} catch (Exception e) {
			logger.error("异常信息:", e);
			return StandardResult.faild(e);
		}
    }

     /**
        * 添加
      *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @PostMapping("/class")
    @RequiresRoles("3")
    @MonitorLog(value = "添加或者修改班级信息",operateType = LogEnum.UPDATE)
    public StandardResult insert(@ModelAttribute ClassModel classModel) {
		try {
            if(StringUtils.isBlank(classModel.getId())){
                classModel.setId(CommonUtils.getUUID());
                classModel.setCreateUserid(CommonUtils.getCurrentUser().getId());
                classModel.setCreateDatetime(new Date());
            }
            classService.insertOrUpdate(classModel);
            return StandardResult.ok();
        } catch (Exception e) {
            logger.error("异常信息:", e);
            return StandardResult.faild(e);
        }
    }

    /**
         * 修改
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @PutMapping("/class")
    public StandardResult updateById(@RequestBody ClassModel classModel) {
        try {
            classService.updateById(classModel);
            return StandardResult.ok();
        } catch (Exception e) {
            logger.error("异常信息:", e);
            return StandardResult.faild(e);
        }
    }

    /**
        * 通过id获取详情
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @GetMapping("/class/{id}")
    public StandardResult selectById(@PathVariable String id) {
		try {
		    if(StringUtils.isNotBlank(id)){
                return StandardResult.ok(classService.selectById(id));
            }else {
		        return StandardResult.faild("班级不能为空");
            }
		} catch (Exception e) {
		     logger.error("异常信息:", e);
		     return StandardResult.faild(e);
		}
    }

    /**
     * 通过id删除数据
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @DeleteMapping("/class/{id}")
    public StandardResult deleteById(@PathVariable String id) {
        try {
            classService.deleteById(id);
            return StandardResult.ok();
        } catch (Exception e) {
            logger.error("异常信息:", e);
            return StandardResult.faild(e);
        }
    }

    /**
     * 根据院系查出对应的班级
     *
     * @author : huangpengfei
     * @since : Create in 2020-04-04
     */
    @GetMapping("/classByFaculty")
    public StandardResult selectByClassesByFaculty(String faculty) {
        try {
            Wrapper<ClassModel> wrapper = new EntityWrapper<>();
            if(StringUtils.isNotBlank(faculty)){
                wrapper.eq(ClassModel.FACULTY,faculty);
            }else {
                return StandardResult.faild("请选择对应的院系");
            }
            return StandardResult.ok(classService.selectList(wrapper));
        } catch (Exception e) {
            logger.error("异常信息:", e);
            return StandardResult.faild(e);
        }
    }

}
